package com.publishproject.test;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.publishproject.MainActivity;
import com.publishproject.R;

import java.lang.reflect.Field;
import java.util.Iterator;

/**
 * @author Android客户端组-tanghongbin
 * @Title: AppbadgeUtil
 * @Package com.publishproject.test
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @date 2017/1/9 10:56
 * @Description: TODO
 */
public class AppbadgeUtil {
    /**
     * 设置桌面Icon角标，当前支持小米，索尼，三星,vivo。
     *
     * @param context
     * @param number
     */
    public static void sendBadgeNumber(Context context, String number) {
        if (TextUtils.isEmpty(number)) {
            number = "0";
        } else {
            int numInt = Integer.valueOf(number);
            number = String.valueOf(Math.max(0, Math.min(numInt, 99)));
        }

        if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
            sendToXiaoMi(context, number);
        } else if (Build.MANUFACTURER.equalsIgnoreCase("sony")) {
            sendToSony(context, number);
        } else if (Build.MANUFACTURER.toLowerCase().contains("samsung")) {
            sendToSamsumg(context, number);
        }else if(Build.MANUFACTURER.toLowerCase().contains("vivo")) {
            sendToVivo(context,Integer.valueOf(number));
        }else {
            Log.e("AppUtils", "sendBadgeNumber: Not Support");
        }
    }


    private static void sendToVivo(Context context,int paramInt) {
        Intent localIntent1 = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
        localIntent1.putExtra("packageName", context.getPackageName());
        localIntent1.putExtra("className", MainActivity.class.getName());
        localIntent1.putExtra("notificationNum", paramInt);
        context.sendBroadcast(localIntent1);
    }


    private final static String lancherActivityClassName = MainActivity.class.getName();

    private static void sendToXiaoMi(Context context, String number) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = null;
        boolean isMiUIV6 = true;
        try {
//            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//            builder.setContentTitle("您有" + number + "未读消息");
//            builder.setTicker("您有" + number + "未读消息");
//            builder.setAutoCancel(true);
//            builder.setSmallIcon(R.mipmap.ic_launcher);
//            builder.setDefaults(Notification.DEFAULT_LIGHTS);
//            notification = builder.build();
            Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
            Object miuiNotification = miuiNotificationClass.newInstance();
            Field field = miuiNotification.getClass().getDeclaredField("messageCount");
            field.setAccessible(true);
            field.set(miuiNotification, Integer.valueOf(number));// 设置信息数
            field = notification.getClass().getField("extraNotification");
            field.setAccessible(true);
            field.set(notification, miuiNotification);
        } catch (Exception e) {
            e.printStackTrace();
            //miui 6之前的版本
            isMiUIV6 = false;
            Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
            localIntent.putExtra("android.intent.extra.update_application_component_name", context.getPackageName() + "/" + lancherActivityClassName);
            localIntent.putExtra("android.intent.extra.update_application_message_text", number);
            context.sendBroadcast(localIntent);
        } finally {
            if (notification != null && isMiUIV6) {
                //miui6以上版本需要使用通知发送
                nm.notify(101010, notification);
            }
        }

    }

    private static void sendToSony(Context context, String number) {
         boolean isShow = true;
        if ("0".equals(number)) {
            isShow = false;
        }
        Intent localIntent = new Intent();
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE", isShow);//是否显示
        localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME", lancherActivityClassName);//启动页
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number);//数字
        localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME", context.getPackageName());//包名
        context.sendBroadcast(localIntent);
    }

    private static void sendToSamsumg(Context context, String number) {
        Intent localIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
        localIntent.putExtra("badge_count", Integer.valueOf(number));//数字
        localIntent.putExtra("badge_count_package_name", context.getPackageName());//包名
        localIntent.putExtra("badge_count_class_name", lancherActivityClassName); //启动页
        context.sendBroadcast(localIntent);
        Log.d("AppUtils", "Samsumg isSendOk" + number);
    }

}
